import MonsterManager from "./MonsterManager";
import BulletManager from "./BulletManager";
import Buttle from "./Buttle";
// import Bullet from "./bullet";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Tower extends cc.Component {
    range = 300;//攻击距离
    target:cc.Node = null//攻击目标
    private isTarget = true
    towerShoot:cc.Node = null//攻击的工具
    bulletSpwan:cc.Node = null//攻击的子弹
    onLoad(){

        this.towerShoot = this.node.children[1]
        this.bulletSpwan = this.towerShoot.children[0]
        //开启监听
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this)
        this.schedule(this.createBullet,0.5,undefined, 0)
    }

    update(){
        const pos = this.node.parent.getPosition()
        //查找在范围内的距离最近的怪物
        const mons = MonsterManager.getInstance().findMinDisMonster(pos, this.range)
        //看向怪物
        this.target = mons
        this.lookAtTarget()

        // if(this.target != null && this.isTarget){
        //     this.schedule(this.createBullet, 0.2,Number.MAX_SAFE_INTEGER, 0);
        //     this.isTarget = false
        // }

        // if(this.target == null && this.isTarget == false){
        //     this.unschedule(this.createBullet)
        //     this.isTarget = true
        // }

        // this.collisionBulletAndEnemy()
    }

    onTouchStart(event:cc.Event.EventTouch){
        //获取触摸点的坐标(世界坐标系)
        const touchPoint = event.getLocation()
        if(!this.node.parent.getBoundingBox().contains(touchPoint)){
            // this.node.getChildByName("range").active = !this.isShowArea
            // this.isShowArea = !this.isShowArea
        }
    }

    lookAtTarget(){
        if(this.target != null){
            //计算向量值
            const offset = this.target.getPosition().sub(this.node.parent.getPosition())
            //求与垂直方向的夹角
            const radian = offset.angle(cc.Vec2.UP)
            //弧度转角度
            let angle = (radian * 180) / Math.PI ;
            //设置旋转角度
            this.towerShoot.angle = offset.x < 0 ? angle : -angle
            // this.scheduleOnce(this.createBullet, 0.2)
        }
    }

    createBullet(){
        if(this.target != null){
            //将bulletSpwan的坐标转到Canvas坐标系下
            let pos = this.bulletSpwan.convertToWorldSpaceAR(cc.Vec2.ZERO)
           
            //创建预制体子弹
            cc.loader.loadRes("prefabs/bullet.prefab", (err, prefab) => {
                //实例化
                let bullet:cc.Node = cc.instantiate(prefab);
                bullet.getComponent(Buttle).target = this.target
                bullet.setPosition(new cc.Vec2(pos.x - 480, pos.y - 320));
                bullet.setParent(cc.find("Canvas"))
                // //子弹向怪物移动
                // let mt = cc.moveTo(0.3, this.target.getPosition())
                // bullet.runAction(mt)
                BulletManager.getInstance().addBullet(bullet)
            })  
        }
    }

    collisionBulletAndEnemy(){
        // //子弹与怪物的碰撞检测
        // MonsterManager.getInstance().monsterArr.forEach(monster => {
        //     BulletManager.getInstance().bulletArr.forEach(bullet => {
        //         let isCarsh = monster.getBoundingBox().intersects(bullet.getBoundingBox())
        //         if(isCarsh){
        //             BulletManager.getInstance().collection(bullet)
        //         }
        //     })
        // })
    }

    onDisable(){
        this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this)
    }
}
